package cn.edu.njust.alg.leetcode;

/**
 * 创建时间：2015年6月15日 上午11:47:37 项目名称：AlgorithmExercise
 * 
 * @author Zhou Xiumin
 * @version 1.0
 * @since JDK 1.7.0_21 文件名称：Leetcode_0_t.java 类说明：
 */
public class Leetcode_201_Bitwise_AND_of_Numbers_Range {

	public static int rangeBitwiseAnd(int m, int n) {
		int p = 0;
		while (m != n) {
			m = m >> 1;
			n = n >> 1;
			p++;
		}

		return m << p;
	}

	public static int rangeBitwiseAnd_(int m, int n) {
		int i = m & n;
		int j = m ^ n;
		int count = 0;
		while (j >> 1 != 0) {
			count++;
			j = j >> 1;
		}
		i = (i >> count) << count;
		return i;
	}

	public static void main(String[] args) {
		long t1 = System.nanoTime();
		System.out.println(rangeBitwiseAnd_(700000000, 2147483641));

		long t2 = System.nanoTime();
		System.out.println(rangeBitwiseAnd(700000000, 2147483641));
		System.out.println(t2 - t1);
		System.out.println(System.nanoTime() - t2);
	}

}
